package com.google.coffeeshop.business;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import vn.bang.utility.JpaUtil;
import vn.bang.utility.StringUtil;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.dao.ConfigurationDAO;
import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.dao.GlobalStatusDAO;
import com.google.coffeeshop.entity.ConfigurationDTO;
import com.google.coffeeshop.entity.GlobalStatusDTO;
import com.google.coffeeshop.log.Log;


public class SystemBusiness extends AbstractBaseBusiness {
	
	static public String getStatusValue(Integer statusId) {
		String value = "";
		
		final EntityManager entityManager = createEntityManager();
		try {
			GlobalStatusDAO globalStatusDAO = DAOFactory.createGlobalStatusDAO(entityManager);
			GlobalStatusDTO globalStatusDTO = globalStatusDAO.find(statusId);
			if (null != globalStatusDTO) {
				value = globalStatusDTO.getValue();
			}
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		
		return value;
	}
	
	static public void saveLoginConfig(final boolean rememberLogin, final boolean autoLogin) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				saveConfig(GeneralConstants.REMEMBER_LOGIN_CONFIG, rememberLogin, entityManager);
				saveConfig(GeneralConstants.AUTO_LOGIN_CONFIG, autoLogin, entityManager);
			JpaUtil.commitEntityManager(entityManager);
			
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	static public void saveLoginRecords(final String username, final String password, final boolean encrypt) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				saveConfig(GeneralConstants.USERNAME_RECORD_CONFIG, username, entityManager);
				saveConfig(GeneralConstants.PASSWORD_RECORD_CONFIG, (encrypt ? encryptMD5(password) : password), entityManager);
			JpaUtil.commitEntityManager(entityManager);
			
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	static public boolean isRememberLogin() {
		boolean rememberLogin = Boolean.FALSE;
		final EntityManager entityManager = createEntityManager();
		try {
			String value = getConfig(GeneralConstants.REMEMBER_LOGIN_CONFIG, entityManager);
			if (!StringUtil.isNullOrEmpty(value)) {
				rememberLogin = convertToBooleanValue(value);
			}
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return rememberLogin;
	}
	
	static public boolean isAutoLogin() {
		boolean autoLogin = Boolean.FALSE;
		final EntityManager entityManager = createEntityManager();
		try {
			String value = getConfig(GeneralConstants.AUTO_LOGIN_CONFIG, entityManager);
			if (!StringUtil.isNullOrEmpty(value)) {
				autoLogin = convertToBooleanValue(value);
			}
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return autoLogin;
	}
	
	static public String getUsernameRecord() {
		String username = "";
		final EntityManager entityManager = createEntityManager();
		try {
			String value = getConfig(GeneralConstants.USERNAME_RECORD_CONFIG, entityManager);
			if (null != value) {
				username = value;
			}
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return username;
	}
	
	static public String getPasswordRecord() {
		String password = "";
		final EntityManager entityManager = createEntityManager();
		try {
			String value = getConfig(GeneralConstants.PASSWORD_RECORD_CONFIG, entityManager);
			if (null != value) {
				password = value;
			}
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return password;
	}
	
	private static void saveConfig(final String name, final Object value, final EntityManager entityManager) {
		ConfigurationDAO configurationDAO = DAOFactory.createConfigurationDAO(entityManager);
		ConfigurationDTO configurationDTO = configurationDAO.findByName(name);
		if (null != configurationDTO) {
			configurationDTO.setValue(convertDbValue(value));
			configurationDAO.merge(configurationDTO);
		} else {
			configurationDAO.persist(createNewConfiguration(name, convertDbValue(value)));
		}
	}
	
	private static ConfigurationDTO createNewConfiguration(final String name, final String value) {
		ConfigurationDTO configurationDTO = new ConfigurationDTO();
		configurationDTO.setName(name);
		configurationDTO.setValue(value);
		return configurationDTO;
	}
	
	private static String convertDbValue(final Object value) {
		if (value instanceof Boolean) {
			return convertBooleanValue((Boolean)value);
		} else if (value instanceof String) {
			return (String)value;
		}
		return "";
	}
	
	private static String convertBooleanValue(final boolean value) {
		if (Boolean.FALSE == value) {
			return GeneralConstants.FALSE_CONFIG_VALUE;
		}
		return GeneralConstants.TRUE_CONFIG_VALUE;
	}
	
	private static boolean convertToBooleanValue(final String value) {
		if (GeneralConstants.FALSE_CONFIG_VALUE.equals(value)) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}
	
}
